package com.company.cloud.thirduser.moudules.utils;

import cn.hutool.json.JSONUtil;
import com.company.cloud.thirduser.moudules.dto.requestDto.SsoAccountInfoRequestDto;
import com.company.cloud.thirduser.moudules.properties.SsoSaasProperties;
import com.company.common.server.aes.AESUtils;
import com.company.common.server.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
public class SsoSaasUtil {

    @Autowired
    SsoSaasProperties ssoSaasProperties;

    private Map<String, String> setHeaders(){
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "text/html");
        headers.put("--uid--", ssoSaasProperties.getClientCode());
        headers.put("--requestId--", UUID.randomUUID().toString().replaceAll("-", ""));
        return headers;
    }

    public String sereachAccount(SsoAccountInfoRequestDto accountInfo){
        String ciphertext = AESUtils.encrypt(JSONUtil.toJsonStr(accountInfo), ssoSaasProperties.getAesKey());
        String rtn = HttpRequest.httpdoPostBodyHeader(ssoSaasProperties.getApiUrlPerfix() + "/api/searchAccount", ciphertext, setHeaders(), null);
        String text = AESUtils.decrypt(rtn, ssoSaasProperties.getAesKey());
        return text;
    }

    public String addAccount(SsoAccountInfoRequestDto accountInfo){
        String ciphertext = AESUtils.encrypt(JSONUtil.toJsonStr(accountInfo), ssoSaasProperties.getAesKey());
        String rtn = HttpRequest.httpdoPostBodyHeader(ssoSaasProperties.getApiUrlPerfix() + "/api/addAccount", ciphertext, setHeaders(), null);
        String text = AESUtils.decrypt(rtn, ssoSaasProperties.getAesKey());
        return text;
    }

    public String midifyAccount(SsoAccountInfoRequestDto accountInfo){
        String ciphertext = AESUtils.encrypt(JSONUtil.toJsonStr(accountInfo), ssoSaasProperties.getAesKey());
        String rtn = HttpRequest.httpdoPostBodyHeader(ssoSaasProperties.getApiUrlPerfix() + "/api/modifyAccount", ciphertext, setHeaders(), null);
        String text = AESUtils.decrypt(rtn, ssoSaasProperties.getAesKey());
        return text;
    }

}
